package pers.cz.logtail.handler;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @program: Reids
 * @description: 线程池
 * 1、维护一个线程池，按照文件名，一个文件只使用一个线程
 * @author: Cheng Zhi
 * @create: 2023-01-09 12:24
 **/
public class FileLineHandleThreadPool {

    private static Map<String, ExecutorService> threadMap = new HashMap<String, ExecutorService>();

    /**
     * 获取一个线程池对象
     * @return
     */
    public static ExecutorService getSimpleThradPool() {
        ExecutorService threadPool = Executors.newCachedThreadPool();
        return threadPool;
    }

    public static ExecutorService getThreadPool(String fileName) {
        ExecutorService executorService = threadMap.get(fileName);
        if (executorService != null) {
            return executorService;
        }

        executorService = Executors.newCachedThreadPool();
        //executorService = Executors.newFixedThreadPool(10);
        threadMap.put(fileName, executorService);

        return executorService;
    }

    public static void stopThreadPool(String flowId) {
        ExecutorService executorService = threadMap.get(flowId);
        executorService.shutdown();
    }
}
